import java.util.Arrays;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User: Administrator
 * Date: 2024-09-28
 * Time: 20:08
 */
public class MyArrayList<T extends Comparable<T>>{
    private Object[] elem;
    private int usedSize;
    private static final int DEFAULT_SIZE = 2;

    //不给定大小的初始化
    public MyArrayList(){
        this.elem = new Object[DEFAULT_SIZE];
    }

    //给定大小的初始化
    public MyArrayList(int initCapacity){
        this.elem = new Object[initCapacity];
    }

    //在末尾插入数据
    public void add(T data){
        isFull();

        this.elem[this.usedSize] = data;
        this.usedSize++;
    }

    //在特定位置插入数据
    public void add(int pos,T data){
        checkBound(pos);

        isFull();

        for (int i = this.usedSize - 1; i >= pos ; i--) {
            this.elem[i + 1] = this.elem[i];
        }
        this.elem[pos] = data;
        this.usedSize++;
    }

    //确认某个特定的值是否存在在顺序表内
    public boolean contains(T toFind){
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i].equals(toFind)){
                System.out.println("该数据存在:" + toFind );
                return true;
            }
        }
        System.out.println("该数据不存在:" + toFind );
        return false;
    }

    //确认某个特定的值是否存在在顺序表内，存在则返回下标
    public int indexOf(T toFind){
        for (int i = 0; i < this.usedSize; i++) {
            if(this.elem[i].equals(toFind)){
                return i;
            }
        }
        System.out.println("该数据不存在:" + toFind );
        return -1;
    }

    //检查顺序表内是否存满，存满则扩容
    public void isFull(){
        if(this.usedSize == this.elem.length){
            this.elem = Arrays.copyOf(this.elem,2 * this.elem.length);
        }
    }

    //获取特定下标的值
    public Object get(int pos){
        checkPos(pos);
        return this.elem[pos];
    }

    //将特定下标的值重新设置
    public void set(int pos,T value){
        checkPos(pos);
        this.elem[pos] = value;
    }

    //检查范围是否越界
    private void checkBound(int pos){
        if(pos < 0 || pos > this.usedSize){
            throw new PosOutOfBoundException(pos + " 位置不合法！");
        }
    }

    //检查范围是否越界
    private boolean checkPos(int pos){
        if(pos < 0 || pos >= this.usedSize){
            throw new PosOutOfBoundException(pos + " 位置不合法！");
        }
        return false;
    }

    public void remove(T toRemove){
        int index = indexOf(toRemove);
        if(index == -1){
            return;
        }
        for (int j = index; j < this.usedSize - 1; j++) {
            this.elem[j] = this.elem[j + 1];
        }
        this.elem[this.usedSize-1] = null;
        this.usedSize--;
    }

    //获取顺序表长度
    public void size(){
        System.out.println("顺序表中共有 " + this.usedSize + " 个数据");
    }

    //清空数据表

    public void clear(){
        for (int i = 0; i < this.usedSize; i++) {
            this.elem[i] = null;
        }
        this.usedSize = 0;
    }

    //打印顺序表
    public void disPlay(){
        for(int i = 0; i < usedSize;i++){
            System.out.print(this.elem[i] + " ");
        }
        System.out.println();
    }
}








